package one.bugu.android.demon.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/12/13
 * Time: 19:48
 * Description:
 */
public class DateUtils {

    /**
     * 使用24小时制时间格式    hh:为12小时格式  HH: 24小时格式
     */
    public final static String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    private final static String FORMAT_HMS = "HH:mm:ss";

    /**
     * 1小时时长
     */
    private final static long LEAD_ONE_HOUR = 3600000L;

    /**
     * 1天时长
     */
    private final static long LEAD_ONE_DAY = 86400000L;
    /**
     * 1月时长
     */
    private final static long LEAD_ONE_MONTH = 2592000000L;

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<>();

    /**
     * 获取时间差距
     *
     * @param time
     * @return
     */
    public static String getLeadTime(String time) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_YMDHMS);
            Date date = dateFormat.parse(time);
            long currentTime = System.currentTimeMillis();
            long targetTime = date.getTime();
            long leadTime = currentTime - targetTime;
            if (leadTime < LEAD_ONE_HOUR) {
                return "1分钟前";
            }
            if (leadTime >= LEAD_ONE_HOUR && leadTime < LEAD_ONE_DAY) {
                return "1小时前";
            }
            if (leadTime >= LEAD_ONE_DAY && leadTime < LEAD_ONE_MONTH) {
                return "1天之前";
            } else {
                return "1月之前";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将时间差转换为时间格式数据
     *
     * @param millis 时间差
     * @return
     */
    public static String getShortTime(long millis) {
        @SuppressLint("SimpleDateFormat") DateFormat formatter = new SimpleDateFormat(FORMAT_HMS);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return formatter.format(calendar.getTime());
    }

    /**
     * 根据秒数转化为时分秒   00:00:00
     */
    public static String secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0)
            return "00:00";
        else {
            //转换为秒
            time = time / 1000;
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    /**
     * 格式化时间
     *
     * @param i
     * @return
     */
    private static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + i;
        } else {
            retStr = "" + i;
        }
        return retStr;
    }


    /**
     * 具体时间转换成时间戳
     */
    public static long getLongTime(String curtime, String model) {
        long time = 0;
        if (TextUtils.isEmpty(curtime)) {
            return time;
        }
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat(model);
        Date date = null;
        try {
            date = simpleDateFormat.parse(curtime);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param lon   long类型时间
     * @param model 时间显示模式
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getStrTime(Long lon, String model) {
        String reStrTime;
        SimpleDateFormat sdf = new SimpleDateFormat(model);
        reStrTime = sdf.format(new Date(lon));
        return reStrTime;
    }

    /**
     * 判断是否是同一天  根据当前时间与5分钟后时间判断是否是同一天
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static boolean isSameDay() {
        //当前Data
        Date date = new Date(System.currentTimeMillis());
        //5分钟后的data
        Date sameDate = new Date(System.currentTimeMillis() + (1000 * 60 * 5));

        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(sameDate);
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        return nowCalendar.get(Calendar.YEAR) == dateCalendar.get(Calendar.YEAR)
                && nowCalendar.get(Calendar.MONTH) == dateCalendar.get(Calendar.MONTH)
                && nowCalendar.get(Calendar.DATE) == dateCalendar.get(Calendar.DATE);
    }

    /**
     * 获取今日已经过去多少秒
     *
     * @return
     */
    public static int getAfterMillis() {
        int timeMillis = 0;
        Calendar calendar = Calendar.getInstance();
        //今日时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //今日分
        int minute = calendar.get(Calendar.MINUTE);
        //今日秒
        int second = calendar.get(Calendar.SECOND);
        timeMillis = hour * 60 * 60 + minute * 60 + second;
        return timeMillis;
    }

    private static long string2Long(String time, String format) {
        try {
            Date date = null;
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = (Date) sdf.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 获取固定天数后的日期
     */
    public static String getBeforeDate(String creatTime, int beforday) {
        String dateTime = "";
        try {
            //固定时间过去的天数
            long beforeMillis = (long) beforday * 24 * 60 * 60 * 1000;
            long aLong = string2Long(creatTime, FORMAT_YMDHMS);
            long total = beforeMillis + aLong;
            dateTime = getStrTime(total, "yyyy-MM-dd HH:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 获取固定天数后的日期
     */
    public static String getBeforeDate(long creatTime, int beforday) {
        String dateTime = "";
        try {
            //固定时间过去的天数
            long beforeMillis = (long) beforday * (24 * 60 * 60 * 1000);
            long aLong = creatTime;
            long total = beforeMillis + aLong;
            dateTime = getStrTime(total, "yyyy-MM-dd HH:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsYesterday(String day) {
        try {
            Calendar pre = Calendar.getInstance();
            Date predate = new Date(System.currentTimeMillis());
            pre.setTime(predate);

            Calendar cal = Calendar.getInstance();
            Date date = null;
            date = getDateFormat().parse(day);
            cal.setTime(date);

            if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
                int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                        - pre.get(Calendar.DAY_OF_YEAR);

                if (diffDay == -1) {
                    return true;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 获取时间格式
     *
     * @return
     */
    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    /**
     * 获取农场倒计时
     * <p>
     * 每到双数点时生成bgt
     *
     * @return 直接返回秒
     */
    public static long getFarmCountTime() {
        long time = 0;
        Calendar calendar = Calendar.getInstance();
        //现在时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //现在分
        int minute = calendar.get(Calendar.MINUTE);
        //现在秒
        int second = calendar.get(Calendar.SECOND);
        if (hour % 2 == 0) {
            //双时,倒计时加1小时
            time = 60 * 60;
        }
        //剩余分钟 + 剩余秒
        time = time + ((59 - minute) * 60) + (59 - second);
        return time;
    }
}
